﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.OleDb;

public partial class ViewControl : System.Web.UI.UserControl
{
    public List<Vinarii> vinarii;
    string grad;
    string vin;
    protected void Page_Load(object sender, EventArgs e)
    {
        Panel1.BackImageUrl = "~/wine_pic2.jpg";
        lbvina.Visible = false;
        naplati.Visible = false;
        inf.Visible = false;

        ServiceNimic s = new ServiceNimic();
        
        int idGrad = s.getFieldNumber();

      
        Gradovi g = new Gradovi();
        grad = g.getCity(idGrad);
        if (grad != null)
        {


            city.Text = "Во градот " + grad + " ги има следниве винарии:";

            ListItem item = new ListItem("");
            lb1.Items.Add(item);
            vinarii = new List<Vinarii>();

            Vinarii_servis vs = new Vinarii_servis();
            vinarii = vs.prikaziVinarii(idGrad);

            int i = 0;


            while (i < vinarii.Count)
            {
                // vinarii.ElementAt<Vinarii>(i);
                Vinarii v = vinarii[i];
                // vin = "Име: " + v.Ime + "   Адреса: " + v.Adresa + "   e-mail: " + v.Email + " Телефон: " + v.Telefon + "  web: " + v.Web;
                ListItem it = new ListItem(v.Ime, i.ToString());
                lb1.Items.Add(it);
                i++;
            }
        }
        else
        {
            city.Text = "Полето на кое се наоѓаш не е град.";
        }



    }
    public List<Vina> list;
    protected void vinarija_Click(object sender, EventArgs e)
    {
        city.Text = "";
        lb1.Visible = false ;
        vinarija.Visible=false;
        lbvina.Visible = true;
        naplati.Visible = true;
        inf.Visible = true;


        ListItem item = new ListItem("");
        lb1.Items.Add(item);
        list = new List<Vina>();

        Vinarii_servis vs = new Vinarii_servis();
        int id = vs.getIDVinarija(lb1.SelectedItem.ToString());

        
        list = vs.prikaziVina(id);

        int i = 0;
        while (i < list.Count)
        {
            
            Vina v = list[i];
          
            ListItem it = new ListItem(v.Ime, i.ToString());
            lbvina.Items.Add(it);
            i++;
        }
        
        
    }
    protected void lb1_SelectedIndexChanged(object sender, EventArgs e)
    {
        city.Text = "";
        inf.Visible = true;
        city.Visible = false;
        Vinarii_servis vs = new Vinarii_servis();
        int idvinarija = vs.getIDVinarija(lb1.SelectedItem.ToString());
        Vinarii v = vs.infoVinarija(lb1.SelectedItem.ToString());
        
        inf.Items.Add(new ListItem("Адреса: "+v.Adresa,"0"));
        inf.Items.Add(new ListItem("E-mail: "+v.Email,"1"));
        inf.Items.Add(new ListItem("Телефон: "+v.Telefon,"2"));
        inf.Items.Add(new ListItem("Web: "+v.Web,"3"));
        
    }

    protected void lbvina_SelectedIndexChanged(object sender, EventArgs e)
    {
        city.Text = "";
        city.Visible = false;
        lbvina.Visible = true;
        inf.Visible = true;
        naplati.Visible = true;
        inf.Items.Clear();
      
        Vinarii_servis vs = new Vinarii_servis();
        int idVino = vs.getIDVino(lbvina.SelectedItem.ToString(), vs.getIDVinarija(lb1.SelectedItem.ToString()));
        Vina vino = vs.infoVino(idVino);
        inf.Items.Add(new ListItem("Сорта: "+vino.sorta, "0"));
        inf.Items.Add(new ListItem("Сорта на грозје: " + vino.sortaGrozje, "1"));
        inf.Items.Add(new ListItem("Алкохол: " + vino.alkohol+" %", "2"));


    }
    protected void naplati_Click(object sender, EventArgs e)
    {
        lbvina.Visible = false;
        naplati.Visible = false;
        city.Text = "";

        
        ServiceNimic s = new ServiceNimic();
        int igrac = s.getActivePlayerIndex();

    
        Random rnd = new Random();

        string n="";  
        int cena = rnd.Next(130,674);
        
        
            Banka bank = new Banka();
            Vinarii_servis vs=new Vinarii_servis();
            int balance = bank.showBalance(igrac);
           
            if (cena > balance)
            {
       

                city.Visible = true;
                city.Text = "Немате доволно средства на вашето салдо! \nЦената на виното за еден литар е " + cena + " МКД";
                inf.Visible = false;
                lb1.Visible = true;
                vinarija.Visible = true;
            }
            else
            {
                bool b = bank.withdrawMoney(igrac, cena);
                city.Visible = true;
                if (b == true)
                {

                    city.Text = "Цената на виното за еден литар е " + cena + " МКД. \nНаплатата е извршена!";
                    inf.Visible = false;
                }
                else
                {
                    city.Text = "error";
                }
            }
      
             


    }
    
}
